package com.itheima.day02_delayqueue_producer.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQDLXConfig {
    //交换机名称
    private static final String EXCHANGE_NAME = "test_exchange_dlx";
    //队列名称
    private static final String QUEUE_NAME = "test_queue_dlx";

    //创建普通交换机
    @Bean("test_exchange_dlx")
    public Exchange test_exchange_dlx(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }

    //创建普通队列，并指定其死信交换机和routing-key
    @Bean("test_queue_dlx")
    public Queue test_queue_dlx(){
        return QueueBuilder.durable(QUEUE_NAME)
                .withArgument("x-message-ttl",10000)
                .withArgument("x-max-length",15)
                .withArgument("x-dead-letter-exchange","exchange_dlx")
                .withArgument("x-dead-letter-routing-key","dlx.aaa")
                .build();
    }

    //创建死信交换机
    @Bean("exchange_dlx")
    public Exchange exchange_dlx(){
        return ExchangeBuilder.topicExchange("exchange_dlx").durable(true).build();
    }

    //创建死信队列
    @Bean("queue_dlx")
    public Queue queue_dlx(){
        return QueueBuilder.durable("queue_dlx").build();
    }

    //死信队列和死信交换机进行绑定
    @Bean
    public Binding queue_exchange_dlx(@Qualifier("queue_dlx") Queue queue, @Qualifier("exchange_dlx") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("dlx.#").noargs();
    }

    //普通队列和普通交换机进行绑定
    @Bean
    public Binding test_queue_exchange_dlx(@Qualifier(QUEUE_NAME) Queue queue, @Qualifier(EXCHANGE_NAME) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("test.dlx.#").noargs();
    }
}
